package whu.cachhe.makeorder.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Adapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import whu.cachhe.makeorder.R;
import whu.cachhe.makeorder.util.Logger;

/**
 * Created by CocoAdapter on 2018/3/23.
 */

public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    protected View footer;
    protected boolean isLoading;

    protected Adapter adapter;
    protected OnLoadMoreListener onLoadMoreListener;

    private int capacity = 10;

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        this.adapter = adapter;
    }

    public Adapter getBindAdapter(){
        return adapter;
    }

    public LoadMoreListView(Context context) {
        super(context);
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        footer = LayoutInflater.from(context).inflate(R.layout.view_loadmorelistview_footer, null);
        footer.findViewById(R.id.footer_layout).setVisibility(GONE);

        addFooterView(footer);
        setOnScrollListener(this);
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        ListAdapter adapter = getAdapter();
        if (adapter == null)
            return;

        if (adapter.getCount() >= capacity && (firstVisibleItem + visibleItemCount) == totalItemCount) {
            View lastVisibleItemView = view.getChildAt(view.getChildCount() - 1);
            if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == view.getHeight()) {
                if (!isLoading) {
                    isLoading = true;
                    footer.findViewById(R.id.footer_layout).setVisibility(VISIBLE);
                    if (onLoadMoreListener != null){
                        onLoadMoreListener.onLoadMore();
                    }
                }
            }
        }
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener){
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public interface OnLoadMoreListener{
        void onLoadMore();
    }

    public void finishLoading(){
        isLoading = false;
        footer.findViewById(R.id.footer_layout).setVisibility(GONE);
    }
}
